package com.ruoyi.project.proxy;

import com.alibaba.fastjson.JSON;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.ruoyi.common.utils.StringUtils;
import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("proxyTask")
public class ProxyTask
{

    @Autowired
    MqttProxyConfig mqttProxyConfig;

    public void checkMQTTState(){
        mqttProxyConfig.check();
    }

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    public void getPLC(String connectString, String tagAddress, String tag)
    {

        String topic_send = "v1/devices/me/telemetry";
        HashMap<String,Object> map= new HashMap<>();

        try (PlcConnection connection = new DefaultPlcDriverManager().getConnection(connectString))
        {
            final PlcReadRequest.Builder builder = connection.readRequestBuilder();
            builder.addTagAddress(tag,tagAddress);
            final PlcReadRequest readRequest = builder.build();
            final PlcReadResponse readResponse = readRequest.execute().get(5, TimeUnit.SECONDS);
            String ret = readResponse.getString(tag);
            map.put(tag,ret==null?"0":ret);
            mqttProxyConfig.publish(topic_send, JSON.toJSONString(map).getBytes()   ,1,false);
        } catch (PlcConnectionException e) {
            map.put(tag,"null");
            mqttProxyConfig.publish(topic_send, JSON.toJSONString(map).getBytes()   ,1,false);
            throw new RuntimeException(e);
        } catch (Exception e) {
            map.put(tag,"null");
            mqttProxyConfig.publish(topic_send, JSON.toJSONString(map).getBytes()   ,1,false);
            throw new RuntimeException(e);
        }

    }

    public void getPLCwithIOT(String plctype, String ip, String address, String datatype,String tag)
    {
        String topic_send = "v1/devices/me/telemetry";
        HashMap<String,Object> map= new HashMap<>();
        String ret;
        //S200_SMART
        S7PLC s7 = new S7PLC(EPlcType.valueOf(plctype),ip);
        try {
            if (datatype.equals("BOOL")) {
                ret = String.valueOf(s7.readBoolean(address));
            } else if (datatype.equals("BYTE")) {
                ret = String.valueOf(s7.readByte(address));
            } else if (datatype.equals("INT16")) {
                ret = String.valueOf(s7.readInt16(address));
            } else if (datatype.equals("UINT16")) {
                ret = String.valueOf(s7.readUInt16(address));
            } else if (datatype.equals("INT32")) {
                ret = String.valueOf(s7.readInt32(address));
            } else if (datatype.equals("UINT32")) {
                ret = String.valueOf(s7.readUInt32(address));
            } else if (datatype.equals("FLOAT")) {
                ret = String.valueOf(s7.readFloat32(address));
            } else if (datatype.equals("DOUBLE")) {
                ret = String.valueOf(s7.readFloat64(address));
            } else {
                ret = "null";
            }
            map.put(tag, ret);
            mqttProxyConfig.publish(topic_send, JSON.toJSONString(map).getBytes(), 1, false);
        }finally {
            s7.close();
        }
    }

}
